package com.whp.yky.yky.service.serviceimpl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.whp.yky.yky.entity.poentity.Datas;
import com.whp.yky.yky.entity.poentity.Person;
import com.whp.yky.yky.entity.poentity.User;
import com.whp.yky.yky.entity.voentity.VoUser;
import com.whp.yky.yky.dao.UserMapper;
import com.whp.yky.yky.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;




    @Override
    public IPage<User> getUserInfo(IPage<User> page ,User user ){
        return page.setRecords(userMapper.getUserInfo(page,user));
    }


    @Override
    public boolean insertUserInfo(User user){

            try {
                int effecteNum= userMapper.insertUserInfo(user);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("增加成功，主键为："+user.getId());
                    return true;
                }else {
                    throw  new RuntimeException("插入信息失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("插入信息失败了:"+e.getMessage());
            }

    }

    @Override
    public boolean updateUserInfo(User user){

        if (user.getId()!= 0 ){

            try {
                int effecteNum= userMapper.updateUserInfo(user);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("修改成功，主键为："+user.getId());
                    return true;
                }else {
                    throw  new RuntimeException("修改失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("修改失败:"+e.getMessage());
            }
        }else{
            throw new RuntimeException("信息不能为空！！！！");
        }

    }




    @Override
    public boolean deleteUserInfo(int id) {

        try {
            //对mysql数据库做删除操作后，影响的行数effecteNum
            int effecteNum= userMapper.deleteUserInfo(id);
            if (effecteNum>0){
                return true;
            }else {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public User login(User user){


        if(userMapper.loginInfo(user)!=null){
            return userMapper.loginInfo(user);
        }else{
            throw new RuntimeException("用户名或密码错误");
        }
    }

    @Override
    public User getUser(User user) {
        return userMapper.getUser(user);
    }

    @Override
    public Person getPersonInfo(int id) {
        return userMapper.getPersonInfo(id);
    }

    @Override
    public boolean updatePersonInfo(Person person) {
        if (person.getId()!= 0 ){

            try {
                int effecteNum= userMapper.updatePersonInfo(person);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("修改成功，主键为："+person.getId());
                    return true;
                }else {
                    throw  new RuntimeException("修改失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("修改失败:"+e.getMessage());
            }
        }else{
            throw new RuntimeException("信息不能为空！！！！");
        }
    }


}
